﻿using System;
using System.IO;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public class EgnExtractorTest
{
    private const string INPUT_FILE_NAME = @"..\..\..\EgnExtractor\persons.txt";

    StringBuilder programOutput;
    [TestMethod]
    public void MainTest()
    {
        StringWriter sw = RedirectConsole();
        EgnExtractor.Main();
        sw.Close();
        StringBuilder expected = new StringBuilder();
        expected.AppendLine("4806182906");
        expected.AppendLine("6203120707");
        expected.AppendLine("6711287769");
        Assert.AreEqual(expected.ToString(), programOutput.ToString());
    }

    private StringWriter RedirectConsole()
    {
        programOutput = new StringBuilder();
        StringWriter sw = new StringWriter(programOutput);
        Console.SetOut(sw);
        return sw;
    }

    [TestMethod]
    public void NoEgnTest()
    {
        File.Delete(INPUT_FILE_NAME);
        StreamWriter testFile = new StreamWriter(INPUT_FILE_NAME);
        string sampleText = @"asjgfasjgfjasjfasmvfmas sfaf jasbjnfbas fasb f sajhdf asf .asfausf ad67467a4f asfsayd51264 1312";
        using (testFile)
        {
            testFile.Write(sampleText);
        }
        programOutput = new StringBuilder();
        StringWriter sw = new StringWriter(programOutput);
        Console.SetOut(sw);
        EgnExtractor.Main();
        sw.Close();

        string actualText = programOutput.ToString();

        Assert.AreEqual("", actualText);
    }

    [TestMethod]
    public void SplitEgnTest()
    {
        File.Delete(INPUT_FILE_NAME);
        StreamWriter testFile = new StreamWriter(INPUT_FILE_NAME);
        string sampleText = @"asjgfasjgfjasjfasmvfmas sfaf 121211
9785 df asf .asfausf ad67467a4f asfsayd51264 1312";
        using (testFile)
        {
            testFile.Write(sampleText);
        }
        programOutput = new StringBuilder();
        StringWriter sw = new StringWriter(programOutput);
        Console.SetOut(sw);
        EgnExtractor.Main();
        sw.Close();

        string actualText = programOutput.ToString();

        Assert.AreEqual("", actualText);

    }

}
